package com.manage.modules.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.manage.modules.utils.MathUtils;
import com.manage.modules.vo.TBaseDataRecordVo;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.manage.modules.bo.TBaseDataTransferAddBo;
import com.manage.modules.bo.TBaseDataTransferQueryBo;
import com.manage.modules.bo.TBaseDataTransferEditBo;
import com.manage.modules.domain.TBaseDataTransfer;
import com.manage.modules.mapper.TBaseDataTransferMapper;
import com.manage.modules.vo.TBaseDataTransferVo;
import com.manage.modules.service.ITBaseDataTransferService;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 数据传输统计Service业务层处理
 *
 * @author wx
 * @date 2021-09-15
 */
@Service
public class TBaseDataTransferServiceImpl extends ServiceImpl<TBaseDataTransferMapper, TBaseDataTransfer> implements ITBaseDataTransferService {

    @Override
    public void updateOrSave(List<TBaseDataTransfer> dataTransferList) {
        List<TBaseDataTransfer> old = list();
        // 数据库直接没有 就新增
        if (old == null || old.size() == 0) {
            saveBatch(dataTransferList);
        }
        dataTransferList.forEach(x -> {
            for (TBaseDataTransfer transfer : old) {
                // 判断这两个相等的就设置ID 设置ID就变成修改了
                if (x.getStationIdC().equals(transfer.getStationIdC()) && x.getTranType().equals(transfer.getTranType())) {
                    x.setId(transfer.getId());
                }
            }
        });
        saveOrUpdateBatch(dataTransferList);
    }

    @Override
    public TBaseDataTransferVo queryById(Long id) {
        TBaseDataTransfer db = this.baseMapper.selectById(id);
        return BeanUtil.toBean(db, TBaseDataTransferVo.class);
    }

    @Override
    public List<TBaseDataTransferVo> queryList(TBaseDataTransferQueryBo bo) {
        LambdaQueryWrapper<TBaseDataTransfer> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getTranType()), TBaseDataTransfer::getTranType, bo.getTranType());
        lqw.eq(StrUtil.isNotBlank(bo.getStationIdC()), TBaseDataTransfer::getStationIdC, bo.getStationIdC());
        List<TBaseDataTransferVo> transferVoList = entity2Vo(this.list(lqw));
        // 计算比率
        for (TBaseDataTransferVo transferVo : transferVoList) {
            transferVo.setInTimeRate(MathUtils.except(transferVo.getInNum(), transferVo.getShouldNum(), 2));
            transferVo.setRealRate(MathUtils.except(transferVo.getRealNum(), transferVo.getShouldNum(), 2));
            transferVo.setMissRate(MathUtils.except(transferVo.getMissNum(), transferVo.getShouldNum(), 2));
            transferVo.setOverTimeRate(MathUtils.except(transferVo.getOverNum(), transferVo.getShouldNum(), 2));
        }
        return transferVoList;
    }

    /**
     * 实体类转化成视图对象
     *
     * @param collection 实体类集合
     * @return
     */
    private List<TBaseDataTransferVo> entity2Vo(Collection<TBaseDataTransfer> collection) {
        List<TBaseDataTransferVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, TBaseDataTransferVo.class))
                .collect(Collectors.toList());
        if (collection instanceof Page) {
            Page<TBaseDataTransfer> page = (Page<TBaseDataTransfer>) collection;
            Page<TBaseDataTransferVo> pageVo = new Page<>();
            BeanUtil.copyProperties(page, pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        return voList;
    }

    @Override
    public Boolean insertByAddBo(TBaseDataTransferAddBo bo) {
        TBaseDataTransfer add = BeanUtil.toBean(bo, TBaseDataTransfer.class);
        validEntityBeforeSave(add);
        return this.save(add);
    }

    @Override
    public Boolean updateByEditBo(TBaseDataTransferEditBo bo) {
        TBaseDataTransfer update = BeanUtil.toBean(bo, TBaseDataTransfer.class);
        validEntityBeforeSave(update);
        return this.updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(TBaseDataTransfer entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return this.removeByIds(ids);
    }
}
